from colorful.fields import RGBColorField
from django.db import models

from Robots.models.RemoteOJ import RemoteOJ
from Submission.config import Verdict


# Create your models here.

class Problem(models.Model):
    title = models.CharField(max_length=100, null=False, blank=False, verbose_name="题目名称")
    time_limit = models.CharField(max_length=20, null=False, blank=False, verbose_name="时间限制")
    memory_limit = models.CharField(max_length=20, null=False, blank=False, verbose_name="内存限制")
    is_spj = models.BooleanField(default=False, null=False, blank=False, verbose_name="是否为spj题目")

    tags = models.ManyToManyField('ProblemTag', related_name='problems', verbose_name='题目标签')

    rating = models.IntegerField(default=0 , null=True, blank=True, verbose_name="题目难度")
    remote_oj = models.ForeignKey(RemoteOJ, null=False, blank=False, default=1, on_delete=models.PROTECT,
                                  related_name="oj_problems", verbose_name="题目来源")
    remote_id = models.CharField(max_length=20, null=False, blank=False, verbose_name="来源ID")

    content = models.JSONField(null=True, blank=True, help_text="格式为json字符串", verbose_name="备注")
    last_updated = models.DateTimeField(auto_now=True, editable=False)

    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now_add=True, verbose_name='更新时间')
    is_delete = models.BooleanField(default=False, verbose_name='删除标记')

    def __str__(self):
        return self.remote_oj.name + " " + self.remote_id

    class Meta:
        ordering = ["id"]
        db_table = "problems"

    @property
    def total_accepted(self):
        return self.problem_submissions.filter(verdict=Verdict.ACCEPTED).count()

    @property
    def total_submitted(self):
        return self.problem_submissions.count()


class ProblemTag(models.Model):
    name = models.CharField(max_length=40, null=False, blank=False, verbose_name="标签信息",unique=True)
    color = RGBColorField(default="#FFFFFF", verbose_name="标签颜色")

    def __str__(self):
        return self.name

    class Meta:
        ordering = ["id"]
